package com.lenin.omena.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.event.LoginEvent;
import com.lenin.omena.client.event.LoginEventHandler;
import com.lenin.omena.client.event.LoginEvent.ActionType;
import com.lenin.omena.client.event.ProduceListChangeEvent;
import com.lenin.omena.client.event.ProduceListChangeEventHandler;
import com.lenin.omena.shared.LoginResult;
import com.lenin.omena.shared.domain.Produce;
import com.lenin.omena.shared.domain.User;

public class UserContext implements LoginEventHandler,
		ProduceListChangeEventHandler {

	private User user;
	private Integer authState = -1;

	private HashMap<String, Widget> helpMessageMap = new HashMap<String, Widget>();
	private List<Widget> helpMessageList = new ArrayList<Widget>();

	public UserContext() {

		LoginEvent.register(AppleMarket.getEventBus(), this);
		ProduceListChangeEvent.register(AppleMarket.getEventBus(), this);

	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getAuthState() {
		return authState;
	}

	public void setAuthState(Integer authState) {
		this.authState = authState;
	}

	public void pushHelpMessage(String key, Widget widget) {
		
		helpMessageMap.put(key, widget);
		helpMessageList.add(widget);

	}

	public Widget popHelpMessage(String key) {

		Widget removedWidget = helpMessageMap.remove(key);
		helpMessageList.remove(removedWidget);

		Widget lastMessage = null;

		if (helpMessageList.size() > 0)
			lastMessage = helpMessageList.get(helpMessageList.size() - 1);


		return lastMessage;

	}
	
	
	public Widget replaceHelpMessage(String key, Widget widget) {
		
		Widget replaceable = helpMessageMap.get(key);
		
		if(replaceable != null) {
			
			helpMessageList.set(helpMessageList.indexOf(replaceable), widget);
			helpMessageMap.put(key, widget);
			
			return helpMessageList.get(helpMessageList.size() - 1);
		
		} else {
			
			return null;
		
		}
		
	}
	

	@Override
	public void onLogin(LoginEvent event) {

		LoginResult result = event.getResult();

		if (result.getStatus() == 0) {

			if (event.getActionType() == ActionType.LOGIN) {

				setUser(result.getUser());
				setAuthState(result.getStatus());

				HelpPanel.HELP_MAP = HelpPanel.HELP_MAP_LOGGED_IN;
				HelpPanel.HELP_DIALOG = HelpPanel.HELP_DIALOG_LOGGED_IN;

			} else {

				setUser(null);
				setAuthState(-1);

				HelpPanel.HELP_MAP = HelpPanel.HELP_MAP_NOT_LOGGED_IN;
				HelpPanel.HELP_DIALOG = HelpPanel.HELP_DIALOG_NOT_LOGGED_IN;

			}

		} else {

			HelpPanel.HELP_MAP = HelpPanel.HELP_MAP_NOT_LOGGED_IN;
			HelpPanel.HELP_DIALOG = HelpPanel.HELP_DIALOG_NOT_LOGGED_IN;

		}

		if (helpMessageMap.get("MAP") != null) {
			AppleMarket.replaceHelpMessage("MAP", HelpPanel.HELP_MAP);
		}
		
		if (helpMessageMap.get("DIALOG") != null) {
			AppleMarket.replaceHelpMessage("DIALOG", HelpPanel.HELP_DIALOG);
		}
	
	}

	@Override
	public void onProduceListChange(ProduceListChangeEvent event) {

		List<Produce> produce = event.getProduce();

		if (user != null && produce != null) {

			user.getSite().setProduce(produce);

		}

	}

}
